package kr.nextree.edu.opendic.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DictionaryData {

    private static DictionaryData dictonaryData;

    private Map<Integer, Dictionary> dictionaryDatas;

    public static DictionaryData getBookInstance() {
        if (dictonaryData == null) {
            dictonaryData = new DictionaryData();
        }
        return dictonaryData;
    }

    public void putDictionary(Dictionary dictionary) {
        if (dictionaryDatas == null) {
            dictionaryDatas = new HashMap<Integer, Dictionary>();
        }
        if (dictionary == null) return;

        dictionaryDatas.put(dictionary.getId(), dictionary);
    }

    public List<Dictionary> getDictories() {
        if (dictionaryDatas == null) return Collections.emptyList();

        List<Dictionary> dictionaies = new ArrayList<Dictionary>();

        Iterator<Integer> iter = dictionaryDatas.keySet().iterator();
        while (iter.hasNext()) {
            dictionaies.add(dictionaryDatas.get(iter.next()));
        }
        return dictionaies;

    }

    public Dictionary getDictionaryById(int id) {
        return this.dictionaryDatas.get(id);
    }

    public int getMaximunKey() {
        if (dictionaryDatas == null) return 1;

        return dictionaryDatas.size() + 1;
    }

}
